package com.ineatconseil.yougo.pages.requesttypes.list;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Property;

import com.ineatconseil.yougo.beans.ActionType;
import com.ineatconseil.yougo.beans.RequestType;
import com.ineatconseil.yougo.dto.RequestTypeDto;
import com.ineatconseil.yougo.exceptions.RestServiceException;
import com.ineatconseil.yougo.pages.AbstractPage;
import com.ineatconseil.yougo.services.rest.RestPath;
import com.sun.jersey.api.client.GenericType;

public class RequestTypesList extends AbstractPage {

	@SuppressWarnings("unused")
	@Property
	private RequestType requestType;

	/**
	 * Get the list.
	 */
	public List<RequestType> getRequestTypes() {

		try {

			List<RequestTypeDto> types =
					getRestServiceInvoker().call(RestPath.getSimplePath(RestPath.PATH_REQUEST_TYPES, ""),
							new GenericType<List<RequestTypeDto>>() {
							});

			// Mapp :
			if (types != null) {
				List<RequestType> toReturn = new ArrayList<RequestType>();
				for (RequestTypeDto currentUser : types) {
					RequestType req = getMapper().map(currentUser, RequestType.class);
					if (!req.isEmpty()) {
						toReturn.add(req);
					}
				}
				if (toReturn.size() == 0) {
					toReturn = null;
				}

				return toReturn;
			}
			return null;

		} catch (RestServiceException e) {
			return null;
		}

	}

	/**
	 * Delete action.
	 */
	@Log
	void onActionFromDelete(long requestTypeId) {

		try {

			this.getRestServiceInvoker().delete(
					RestPath.getSimplePath(RestPath.PATH_REQUEST_TYPES, String.valueOf(requestTypeId)));

			this.setActionMessageOk("requesttype", ActionType.DELETE);

		} catch (RestServiceException e) {

		}

	}

}
